﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;

using System.Text;
using System.Windows.Forms;
using DAO;
using BUS;
namespace ProductManagement
{
    public partial class DoiVo : Form
    {
        DataTable dtGas;
        DataTable dtLoaiGas;
        public DoiVo()
        {
            InitializeComponent();
            dtGas = new DataTable();
            dtGas.Columns.Add(new DataColumn("STT", typeof(int)));
            dtGas.Columns.Add(new DataColumn("MaGasDoi", typeof(int)));
            dtGas.Columns.Add(new DataColumn("MaGasNhan", typeof(int)));
            dtGas.Columns.Add(new DataColumn("LoaiVoDoi", typeof(String)));
            dtGas.Columns.Add(new DataColumn("SoVoTon", typeof(int)));
            dtGas.Columns.Add(new DataColumn("LoaiVoNhan", typeof(String)));
            dtGas.Columns.Add(new DataColumn("SoLuong", typeof(int)));

        }

        private void dgvVoBinhGas_SelectionChanged(object sender, EventArgs e)
        {
            if (dgvGas.SelectedRows.Count > 0)
                btnXoa.Enabled = true;
            else
                btnXoa.Enabled = false;
        }
        private void btnThem_Click(object sender, EventArgs e)
        {
            if (cboVoDoi.SelectedIndex >= 0 && cboVoNhan.SelectedIndex >= 0)
            {
                if ((int)cboVoDoi.SelectedValue == (int)cboVoNhan.SelectedValue)
                {
                    MessageBox.Show("Vỏ đổi và vỏ nhận trùng nhau", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    return;
                }
                int soVoTon = 0;
                foreach (DataRow aRow in dtLoaiGas.Rows)
                {
                    if ((int)aRow["MaVo"] == (int)cboVoDoi.SelectedValue)
                    {
                        soVoTon = int.Parse(aRow["SoLuong"].ToString());
                    }
                }
                int soLuong = int.Parse(txtSoLuong.Value.ToString());
                DataRow curRow = null;
                foreach (DataRow aRow in dtGas.Rows)
                {
                    if ((int)aRow["MaGasDoi"] == (int)cboVoDoi.SelectedValue)
                    {
                        soLuong += (int)aRow["SoLuong"];
                        curRow = aRow;
                        break;
                    }
                }
                if (soVoTon < soLuong)
                {
                    MessageBox.Show("Không đủ vỏ " + GetTenGas(cboVoDoi.Text) + " để đổi", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                }                    
                else
                {
                    if (curRow == null)
                    {
                        DataRow newRow = dtGas.NewRow();
                        newRow["STT"] = dtGas.Rows.Count + 1;
                        newRow["MaGasDoi"] = cboVoDoi.SelectedValue;
                        newRow["MaGasNhan"] = cboVoNhan.SelectedValue;
                        newRow["SoLuong"] = soLuong;
                        newRow["LoaiVoDoi"] = GetTenGas(cboVoDoi.Text);
                        newRow["LoaiVoNhan"] = cboVoNhan.Text;
                        newRow["SoVoTon"] = soVoTon;
                        dtGas.Rows.Add(newRow);
                        btnCapNhat.Enabled = true;
                    }
                    else
                        curRow["SoLuong"] = soLuong;
                }
            }
        }

        private void btnXoa_Click(object sender, EventArgs e)
        {
            foreach (DataGridViewRow aRow in dgvGas.SelectedRows)
            {
                dtGas.Rows.RemoveAt(aRow.Cells[0].RowIndex);
            }
            if (dtGas.Rows.Count == 0)
                btnCapNhat.Enabled = false;
        }

        private void btnCapNhat_Click(object sender, EventArgs e)
        {
            try
            {
                foreach (DataRow aRow in dtGas.Rows)
                {
                    int voDoi = (int)aRow["MaGasDoi"];
                    int voNhan = (int)aRow["MaGasNhan"];
                    int soLuong = (int)aRow["SoLuong"];
                    VoBinhGasBUS.DoiVo(voDoi, voNhan, soLuong);
                }
                MessageBox.Show("Cập nhật dữ liệu thành công", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show("Cập nhật dữ liệu không thành công\r\n" + ex.Message, "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
        }
        private String GetTenGas(String str)
        {
            int i1 = str.LastIndexOf('(');
            if (i1 >= 0)
            {
                int i2 = str.LastIndexOf(')');
                return str.Substring(0, i1 - 1);
            }
            return str;
        }
        private void DoiVo_Load(object sender, EventArgs e)
        {
            try
            {
                String strSql = "select vo.LoaiGas as MaVo, TenLoai & ' (' & sum(vo.SoLuong) & ')' as LoaiGas, sum(vo.SoLuong) as SoLuong from VoBinhGas vo, LoaiGas loai where vo.LoaiGas=loai.ID group by vo.LoaiGas, loai.TenLoai";
                dtLoaiGas = DataAccess.Query(strSql);
                cboVoDoi.ValueMember = "MaVo";
                cboVoDoi.DisplayMember = "LoaiGas";
                cboVoDoi.DataSource = dtLoaiGas;

                strSql = "select ID as MaVo, TenLoai from LoaiGas";
                DataTable dt = DataAccess.Query(strSql);
                cboVoNhan.ValueMember = "MaVo";
                cboVoNhan.DisplayMember = "TenLoai";
                cboVoNhan.DataSource = dt;

                dgvGas.AutoGenerateColumns = false;
                dgvGas.DataSource = dtGas;
            }
            catch (Exception ex) {
                MessageBox.Show("Không thể load form\r\n" + ex.Message, "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                Close();
            }
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
